package com.property.demo.security;

import cn.dev33.satoken.stp.StpInterface;
import com.property.demo.model.Permission;
import com.property.demo.model.Role;
import com.property.demo.model.RolePermission;
import com.property.demo.model.UserRole;
import com.property.demo.repository.PermissionRepository;
import com.property.demo.repository.RolePermissionRepository;
import com.property.demo.repository.RoleRepository;
import com.property.demo.repository.UserRoleRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 自定义权限验证接口扩展
 */
@Component
public class StpInterfaceImpl implements StpInterface {

    @Autowired
    private UserRoleRepository userRoleRepository;
    
    @Autowired
    private RoleRepository roleRepository;
    
    @Autowired
    private PermissionRepository permissionRepository;
    
    @Autowired
    private RolePermissionRepository rolePermissionRepository;

    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 获取用户ID
        Long userId = Long.parseLong(loginId.toString());
        
        // 获取用户角色
        List<UserRole> userRoles = userRoleRepository.findByUserId(userId);
        List<Long> roleIds = userRoles.stream()
            .map(UserRole::getRoleId)
            .collect(Collectors.toList());
        
        // 如果没有角色，返回空列表
        if (roleIds.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 获取角色对应的权限
        List<String> permissionCodes = new ArrayList<>();
        for (Long roleId : roleIds) {
            List<RolePermission> rolePermissions = rolePermissionRepository.findByRoleId(roleId);
            List<Long> permissionIds = rolePermissions.stream()
                .map(RolePermission::getPermissionId)
                .collect(Collectors.toList());
            
            if (!permissionIds.isEmpty()) {
                List<Permission> permissions = permissionRepository.findAllById(permissionIds);
                List<String> codes = permissions.stream()
                    .map(Permission::getCode)
                    .collect(Collectors.toList());
                
                permissionCodes.addAll(codes);
            }
        }
        
        return permissionCodes;
    }

    /**
     * 返回一个账号所拥有的角色标识集合
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        // 获取用户ID
        Long userId = Long.parseLong(loginId.toString());
        
        // 获取用户角色
        List<UserRole> userRoles = userRoleRepository.findByUserId(userId);
        List<Long> roleIds = userRoles.stream()
            .map(UserRole::getRoleId)
            .collect(Collectors.toList());
        
        // 如果没有角色，返回空列表
        if (roleIds.isEmpty()) {
            return new ArrayList<>();
        }
        
        // 获取角色代码
        List<Role> roles = roleRepository.findAllById(roleIds);
        return roles.stream()
            .map(Role::getCode)
            .collect(Collectors.toList());
    }
} 